package member;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

public class Login {
	private String memberid = "";
	private String pwd = "";
	private String newPwd1 = "";
	private String irum = "";
	private String iden_number = "";
	private String team = "";
	private String position = "";
	private String phone = "";
	private String email = "";
	private String address = "";
	
	// -------------------------------------------------
	// JDBC 연결
	// -------------------------------------------------
	
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	String db_user = "hr";
	String db_pwd = "hr";
	
	Connection conn;

	

	public Login(){

		try{
			//1) 드라이버 로딩
			Class.forName(driver).newInstance();

			//2) Connection 객체 생성
			conn = DriverManager.getConnection(url, db_user, db_pwd);

			//3) PreparedStatement 작성
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public boolean login(){
		boolean flag=false;
		try{
			String sql="select * from memberdata where memberid=? and pwd=?";
			//3) PreparedStatement 작성
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, memberid);
			ps.setString(2, pwd);
			
			//4)ResultSet 생성
			ResultSet rs = ps.executeQuery();
			
			if(rs.next()){
				flag = true;
			}else{
				flag = false;
			}
			ps.close();conn.close();
			
		}catch(Exception e){

		}
		return flag;
	}
	
	public Vector list(){
		Vector<MemberVo> v = new Vector<>();   
		String sql = "select * from memberdata where memberid like ?";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, "%"+memberid+"%");
			ResultSet rs = ps.executeQuery();
			
			while(rs.next()){
				MemberVo m = new MemberVo(rs.getString("memberid"), rs.getString("pwd"),
						 rs.getString("irum"), rs.getString("hiredate"),
						 rs.getString("iden_number"), rs.getString("team"),
						 rs.getString("position"), rs.getString("phone"),
						 rs.getString("email"), rs.getString("address"));
				v.add(m);
			}
			
			rs.close();
			ps.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return v;
	}
/*	public Vector booklist(){
		Vector<GuestVo> v = new Vector<>();   
		String sql = "select * from guestbook";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
		//	ps.setString(1, "%"+memberid+"%");
			ResultSet rs = ps.executeQuery();
			
			while(rs.next()){
				GuestVo m = new GuestVo(rs.getString("serial"), rs.getString("mid"),
						 rs.getString("subject"), rs.getString("content"),
						 rs.getString("mdate"));
				v.add(m);
			}
			
			rs.close();
			ps.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return v;
	}*/
	public MemberVo view(){
		MemberVo m = null;
		String sql = "select * from memberdata where memberid=?";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, memberid);
			ResultSet rs = ps.executeQuery();
			
			if(rs.next()){
				m = new MemberVo(rs.getString("memberid"), rs.getString("pwd"),
								 rs.getString("irum"), rs.getString("hiredate"),
								 rs.getString("iden_number"), rs.getString("team"),
								 rs.getString("position"), rs.getString("phone"),
								 rs.getString("email"), rs.getString("address"));
				
			}
			rs.close();
			ps.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return m;
	}
	
	public boolean modify(){
		boolean flag=false;
		String sql = "update memberdata set pwd=? where memberid=? and pwd=?";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, newPwd1);
			ps.setString(2, memberid);
			ps.setString(3, pwd);
			
			int cnt = ps.executeUpdate();
			if(cnt > 0)	flag=true;
		
			/*ps.close();
			conn.close();*/
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return flag;
	}
	public boolean delete(){
		boolean flag=false;
		String sql = "delete from memberdata where memberid=? and pwd=?";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, memberid);
			ps.setString(2, pwd);
			
			int cnt = ps.executeUpdate();
			if(cnt > 0) flag=true;
			
			/*ps.close();
			conn.close();*/
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return flag;
	}
	public boolean join(){
		boolean flag=false;
		String sql = "insert into memberdata(memberid, pwd, irum, iden_number, team, position, phone, email, address) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, memberid);
			ps.setString(2, pwd);
			ps.setString(3, irum);
			ps.setString(4, iden_number);
			ps.setString(5, team);
			ps.setString(6, position);
			ps.setString(7, phone);
			ps.setString(8, email);
			ps.setString(9, address);
			
			int cnt = ps.executeUpdate();
			if(cnt > 0) flag=true;
			
			/*ps.close();
			conn.close();*/
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}

	public String getIrum() {
		return irum;
	}
	public void setIrum(String irum) {
		this.irum = irum;
	}
	public String getIden_number() {
		return iden_number;
	}
	public void setIden_number(String iden_number) {
		this.iden_number = iden_number;
	}
	public String getTeam() {
		return team;
	}
	public void setTeam(String team) {
		this.team = team;
	}
	public String getPosition() {
		return position;
	}
	public void setPosition(String position) {
		this.position = position;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getMemberid() {
		return memberid;
	}
	public void setMemberid(String memberid) {
		this.memberid = memberid;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getNewPwd1() {
		return newPwd1;
	}
	public void setNewPwd1(String newPwd1) {
		this.newPwd1 = newPwd1;
	}
	

}
